<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
namespace ApplicationsModule\GalleryModule;

use Nette\Object;
use \SimpleXMLElement;

use ApplicationsModule\GalleryModule\PicasaTokenNotValidException;

/**
 * Description of PicasaWebLite
 *
 * @author honza
 */
class PicasaWebLite extends Object{
    
    const XMLNS_GPHOTO = 'http://schemas.google.com/photos/2007';
    const XMLNS_MEDIA = 'http://search.yahoo.com/mrss/';
    
    public function load($url, $token) {
        $urlToken = $this->appendToken($url, $token);
        $ch = curl_init($urlToken);
        
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        
        $response = curl_exec($ch);
        $info = curl_getinfo($ch);
        
        //TODO: docasne
        if($info["http_code"] == 403) {
            throw new PicasaTokenNotValidException("Token is not valid", 403);
        }
        curl_close($ch);
        //$children = $xml->children("http://schemas.google.com/photos/2007");
        //return $children;
        return $response;
    }
    
    public function loadAlbumSamples($url, $token) {
        $albums = $this->loadAlbums($url, $token);
        return $albums;
    }
    
    public function loadAlbums($url, $token) {
        $response = $this->load($url, $token);
        $xml = $this->makeXML($response);
        $albums = array();
        foreach($xml->entry as $entry) {
            $albums[] = $this->setAlbumAttributes($entry);
        }
        //$children = $xml->children(self::XMLNS_GPHOTO);
        return $albums;
    }
    
    public function loadPhotosFromAlbum($url, $token) {
        $data = $this->load($url, $token);
        $xml = $this->makeXML($data);
        $photos = array();
        foreach($xml->entry as $entry) {
            $photos[] = $this->setPhotoAttributes($entry);
        }
        return $photos;
    }
    
    private function appendToken($url, $token) {
        $url = $url . "?oauth_token=" . $token;
        return $url;
    }
    
    private function makeXML($data) {
        $xml = new SimpleXMLElement($data);
        return $xml;
    }
    
    private function setAlbumAttributes($entry) {
        $album = new PicasaAlbum();
        
        $album->setTitle($entry->title->__toString());
        $album->setService("picasa");
        
        $entryItemPhoto = $entry->children(self::XMLNS_GPHOTO);
        $album->setId($entryItemPhoto->id->__toString());
        $album->setName($entryItemPhoto->name->__toString());
        $album->setTimestamp($entryItemPhoto->timestamp->__toString());
        $album->setNumOfPhotos($entryItemPhoto->numphotos->__toString());
        $album->setIdUser($entryItemPhoto->user->__toString());
        $album->setCommentingEnabled($entryItemPhoto->commentingEnabled->__toString());
        $album->setCommentCount($entryItemPhoto->commentCount->__toString());
        
        
        $entryItemMedia = $entry->children(self::XMLNS_MEDIA);        
        $content = $entryItemMedia->xpath('media:content');
        $album->setContentUrl($this->getAttribute("url", $content, 0));
        
        $description = $entryItemMedia->xpath('media:description');
        $album->setDescription($description[0]->__toString());
        $thumbnail = $entryItemMedia->xpath('media:thumbnail');
        $album->setThumbnail($this->getAttribute("url", $thumbnail, 0));
        
        return $album;
    }
    
    private function setPhotoAttributes($entry) {
        $photo = new PicasaPhoto();
        $photo->setTitle($entry->title->__toString());
        $photo->setService("picasa");
        
        $entryItemPhoto = $entry->children(self::XMLNS_GPHOTO);
        $photo->setId($entryItemPhoto->id->__toString());
        $photo->setName($photo->getTitle());
        $photo->setTimestamp($entryItemPhoto->timestamp->__toString());
        $photo->setCommentingEnabled($entryItemPhoto->commentingEnabled->__toString());
        $photo->setCommentCount($entryItemPhoto->commentCount->__toString());
        
        $entryItemMedia = $entry->children(self::XMLNS_MEDIA);
        
        $entryItemMedia = $entry->children(self::XMLNS_MEDIA);        
        $content = $entryItemMedia->xpath('media:content');
        $photo->setContentUrl($this->getAttribute("url", $content, 0));
        
        $description = $entryItemMedia->xpath('media:description');
        $photo->setDescription($description[0]->__toString());
        $thumbnail = $entryItemMedia->xpath('media:thumbnail');
        $photo->setThumbnail($this->getAttribute("url", $thumbnail, 2));
        
        return $photo;
    }
    
    private function getAttribute($attr, $xpath, $index) {
        $attributes = $xpath[$index]->attributes();
        return $attributes[$attr]->__toString();
    }
}

?>
